/* Developed by Ertan Tike (ertan.tike@moreum.com) */
/* Developed by Aknazarov Zagir (aknazarov@gmail.com) */

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;

namespace SimpleDayView
{
    using AppointmentList = System.Collections.Generic.List<Appointment>;

    public class Appointment
    {
        public Appointment()
        {
            Color = Color.White;
            BorderColor = Color.Blue;
            Title = "New Appointment";
            TextColor = Color.Black;
        }

        public int CellIndex { get; set; }

        public DateTime StartDate { get; set; }

        public DateTime EndDate { get; set; }

        public bool Locked { get; set; }

        public Color Color { get; set; }

        public Color TextColor { get; set; }

        public Color BorderColor { get; set; }

        public string Title { get; set; }

        public string Description { get; set; }

        public virtual void DrawAppointment(Graphics g, Rectangle rect, bool isSelected, int gripWidth, Font baseFont)
        {
            var strFormat = new StringFormat();

            strFormat.Alignment = StringAlignment.Near;
            strFormat.LineAlignment = StringAlignment.Near;

            var startColor = Interpolator.InterpolateColors(Color, Color.White, 0.4f);
            var endColor = Interpolator.InterpolateColors(Color, Color.FromArgb(191, 210, 234), 0.7f);

            // Draw back
            using (var aGb = new LinearGradientBrush(rect, startColor, endColor, LinearGradientMode.Vertical))
                g.FillRectangle(aGb, rect);

            if (isSelected)
                DrawSelected(g, rect);
            else
                DrawNotSelected(startColor, endColor, g, rect, gripWidth);

            rect.X += gripWidth;
            g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            g.DrawString(Title, baseFont, SystemBrushes.WindowText, rect, strFormat);
            g.TextRenderingHint = TextRenderingHint.SystemDefault;
        }

        private void DrawNotSelected(Color start, Color end, Graphics g, Rectangle rect, int gripWidth)
        {
            rect.Width = gripWidth + 1;
            start = Interpolator.InterpolateColors(BorderColor, Color, 0.2f);
            end = Interpolator.InterpolateColors(BorderColor, Color.White, 0.6f);
            using (var aGb = new LinearGradientBrush(rect, start, end, LinearGradientMode.Vertical))
                g.FillRectangle(aGb, rect);
            using (var pen = new Pen(SystemColors.WindowFrame, 1))
                g.DrawRectangle(pen, rect);
            int xLeft = rect.X + 6;
            int xRight = rect.Right + 1;
            int yTop = rect.Y + 1;
            int yButton = rect.Bottom + 1;
            for (int i = 0; i < 5; i++)
            {
                using (var shadowPen = new Pen(Color.FromArgb(70 - 12 * i, Color.Black)))
                {
                    g.DrawLine(shadowPen, xLeft + i, yButton + i, xRight + i - 1, yButton + i);
                    g.DrawLine(shadowPen, xRight + i, yTop + i, xRight + i, yButton + i);
                }
            }
        }

        private void DrawSelected(Graphics g, Rectangle rect)
        {
            using (Pen m_Pen = new Pen(BorderColor, 4))
                g.DrawRectangle(m_Pen, rect);
            rect.Inflate(2, 2);
            using (Pen m_Pen = new Pen(SystemColors.WindowFrame, 1))
                g.DrawRectangle(m_Pen, rect);
            rect.Inflate(-4, -4);
            using (Pen m_Pen = new Pen(SystemColors.WindowFrame, 1))
                g.DrawRectangle(m_Pen, rect);
        }
    }
}
